package com.hwadee.teachingprocessmanagementsystem.service.base.impl;

import com.hwadee.teachingprocessmanagementsystem.entity.Textbook;
import com.hwadee.teachingprocessmanagementsystem.dao.TextbookDao;
import com.hwadee.teachingprocessmanagementsystem.service.base.TextbookService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import jakarta.annotation.Resource;

/**
 * 教材表(Textbook)表服务实现类
 *
 * @author makejava
 * @since 2024-07-11 14:20:03
 */
@Service("textbookService")
public class TextbookServiceImpl implements TextbookService {
    @Resource
    private TextbookDao textbookDao;

    /**
     * 通过ID查询单条数据
     *
     * @param textbookId 主键
     * @return 实例对象
     */
    @Override
    public Textbook queryById(Integer textbookId) {
        return this.textbookDao.queryById(textbookId);
    }

    /**
     * 分页查询
     *
     * @param textbook 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Textbook> queryByPage(Textbook textbook, PageRequest pageRequest) {
        long total = this.textbookDao.count(textbook);
        return new PageImpl<>(this.textbookDao.queryAllByLimit(textbook, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param textbook 实例对象
     * @return 实例对象
     */
    @Override
    public Textbook insert(Textbook textbook) {
        this.textbookDao.insert(textbook);
        return textbook;
    }

    /**
     * 修改数据
     *
     * @param textbook 实例对象
     * @return 实例对象
     */
    @Override
    public Textbook update(Textbook textbook) {
        this.textbookDao.update(textbook);
        return this.queryById(textbook.getTextbookId());
    }

    /**
     * 通过主键删除数据
     *
     * @param textbookId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer textbookId) {
        return this.textbookDao.deleteById(textbookId) > 0;
    }
}
